# -*- coding: utf-8 -*-
"""
Created on Thu Jun 18 10:47:28 2020

@author: Farman
"""

import GeotiffMan

tiff_china = r'clscmp_test_data/N37E120_2010_landcover.tif'
tiff_usa   = r'clscmp_test_data/N37E120-ESACCI-LC-L4-LCCS-Map-300m-P1Y-2010-v2.0.7.tif'

geo_china = GeotiffMan.GeoTiffMan(tiff_china)
geo_usa   = GeotiffMan.GeoTiffMan(tiff_usa)

#geo_china.show_info()
#geo_usa.show_info()

data_china = geo_china.read_band(1)
data_usa   = geo_usa.read_band(1)

row, column = data_usa.shape


def histogram(array):
    a = array.copy()
    rows, columns = a.shape
    a = a.reshape((1, rows*columns))[0].tolist()
    a.sort()
    
    histo = {}
    
    for v in a:
        if histo.get(v) == None:
            histo.update({v:a.count(v)})
    
    return histo


def get_unique_value_list(array):
    histo = histogram(array)
    uvs = sorted(histo.keys())
    return uvs

print(get_unique_value_list(data_china))
print(get_unique_value_list(data_usa))
print(geo_china.get_color_table(1))

def get_most_count(array):
    a = array.copy()
    rows, columns = a.shape
    a = a.reshape((1, rows*columns))[0].tolist()
    a.sort()
    
    unique_values = {}
    
    for v in a:
        if unique_values.get(v) == None:
            unique_values.update({v:a.count(v)})
    
    biggest = None
    
    for k in unique_values.keys():
        if biggest == None:
            biggest = k
        else:
            if unique_values.get(k) > unique_values.get(biggest):
                biggest = k
    
    return biggest



for r in range(359, row):
    break
    for c in range(column):
        cells = data_china[r*10:(r+1)*10, c*10:(c+1)*10]
        pixel = data_usa[r, c]
        print(get_unique_value_list(cells))
        '''
        print('row', r, 'column', c)
        print(cells)
        print(get_most_count(cells))
        print(histogram(cells))
        print(pixel)
        '''
        input('Press Enter to continue ...')
    break

